package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Teacher;
import com.itheima.edu.info.manager.service.TeacherService;

import java.util.Scanner;

public abstract class BaseTeacherController {


    private Scanner sc = new Scanner(System.in);
    private TeacherService teacherService = new TeacherService();

    public void start() {
        loop:while(true){
            System.out.println("--------欢迎来到 <老师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师  5.退出");
            Scanner scanner = new Scanner(System.in);
            String s = scanner.nextLine();
            switch(s){
                case "1":
                    //System.out.println("1.添加老师");
                    addTeacher();
                    break;
                case "2":
                    //System.out.println("2.删除老师");
                    deleteTeacherByid();
                    break;
                case "3":
                    //System.out.println("3.修改老师");
                    updateTeacher();
                    break;
                case "4":
                    //System.out.println("4.查看老师");
                    findAllTeacher();
                    break;
                case "5":
                    break loop;
                default:
                    System.out.println("您的输入有误,请重新输入!!");
                    break;

            }
        }

    }

    //添加老师
    public final void addTeacher() {
        //1.输入学生信息
        String id;
        while(true){
            System.out.println("请输入老师id：");
            id = sc.next();
            //1.将id传到service层中,得到返回结果
            //TeacherService teacherService = new TeacherService();
            boolean exist = teacherService.isExist(id);
            //2.判断结果
            if(exist){
                System.out.println("学号已经创建了,请重新再试");
            }else{
                break;
            }
        }

      /*  System.out.println("请输入老师姓名:");
        String name = sc.next();
        System.out.println("请输入老师年龄:");
        String age = sc.next();
        System.out.println("请输入老师生日:");
        String birthday = sc.next();
        //2.封装成老师对象
        Teacher teacher = new Teacher(id, name, age, birthday);*/

        Teacher teacher = inputInfo(id);


        //3.将对象传到service层,并得到结果
        TeacherService teacherService = new TeacherService();
        boolean result = teacherService.addTeacher(teacher);
        if(result){
            System.out.println("添加成功");
        }else{
            System.out.println("添加失败");
        }


    }

    //删除老师
    public final void deleteTeacherByid() {
        /*String delId;
        while(true){
            //1.键盘录入要删除的学生id
            System.out.println("请输入要删除的老师id:");
            delId = sc.next();
            //2.判断id在数组中的id是否一样
            boolean exist = teacherService.isExist(delId);
            if(!exist){
                System.out.println("您输入的id不存在,请重新输入:");
            }else{
                break;
            }
        }*/

        String id = inputId();

        //3.调用业务员中的deleteTeacherByid根据id,删除学生
        teacherService.deleteTeacherByid(id);
        //4.提示删除成功
        System.out.println("删除成功");

    }

    //修改老师
    public final void updateTeacher() {
        /*String updateId;
        while(true){
            //1.键盘录入要删除的学生id
            System.out.println("请输入要修改的老师id:");
            updateId = sc.next();
            //2.判断id在数组中的id是否一样
            boolean exist = teacherService.isExist(updateId);
            if(!exist){
                System.out.println("您输入的id不存在,请重新输入:");
            }else{
                break;
            }
        }*/


        String updateId = inputId();

        /*System.out.println("请输入新的老师姓名:");
        String name = sc.next();
        System.out.println("请输入新的老师年龄:");
        String age = sc.next();
        System.out.println("请输入新的老师生日:");
        String birthday = sc.next();
        Teacher newTea = new Teacher(updateId, name, age, birthday);*/

        Teacher newTea = inputInfo(updateId);

        //3.将要修改的老师id和新的老师对象传到业务层
        teacherService.updateTeacher(updateId,newTea);
        //4.提示修改完成
        System.out.println("修改成功");

    }

    //查看老师
    public final void findAllTeacher() {
        //1.通过service层传过来时的数组,打印学生信息
        TeacherService teacherService = new TeacherService();
        Teacher[] allTeacher = teacherService.findAllStudent();
        //2.判断传回的学生对象数组是有没有学生数据
        if(allTeacher==null){
            System.out.println("查无信息,请添加数据后再来查询");
            return;
        }

        //打印表头
        System.out.println("学号\t\t姓名\t年龄\t生日");
        for (int i = 0; i < allTeacher.length; i++) {
            //确保数组对象元素都有数据
            if(allTeacher[i]!=null){
                System.out.println(allTeacher[i].getId()+"\t"+allTeacher[i].getName()+"\t"+allTeacher[i].getAge()+"\t\t"+allTeacher[i].getBirthday());
            }
        }


    }

    //输入老师id
    public final String inputId(){
        String id;
        while(true){
            //1.键盘录入要删除的学生id
            System.out.println("请输入老师id:");
            id = sc.next();
            //2.判断id在数组中的id是否一样
            boolean exist = teacherService.isExist(id);
            if(!exist){
                System.out.println("您输入的id不存在,请重新输入:");
            }else{
                break;
            }
        }
        return id;
    }
    
    //输入老师信息
    public abstract Teacher inputInfo(String id);
}
